package cn.xgj.sfzy.controller;

import cn.xgj.sfzy.entity.Consult;
import cn.xgj.sfzy.service.ConsultService;
import cn.xgj.sfzy.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 张卫姣
 * @Date 2024年10月24日 18:14
 * @Version 1.0
 */
@RestController
@RequestMapping("/consult")
public class ConsultController {
    @Autowired
    private ConsultService consultService;

    @PostMapping("/addConsult")
    public Result addConsult(@RequestBody Consult consult){
        Integer num = consultService.add(consult);
        System.out.println(consult.toString());
        if (num!=0){
            return Result.success("添加成功！");
        }else{
            return Result.error("添加失败！");
        }

    }

    @DeleteMapping("/removeConsult/{consultId}")
    public Result removeConsult(@PathVariable("consultId") Long consultId){
        Integer num = consultService.remove(consultId);
        if (num==0){
            return Result.error("删除失败！");
        }else if(num==-1){
            return Result.error("该类型相关数据未删除，请删除之后再试！");
        }
        else{
            return Result.success("删除成功！");
        }

    }

    @PutMapping("/editConsult")
    public Result editConsult(@RequestBody Consult consult){
        Integer num = consultService.edit(consult);
        if (num!=0){
            return Result.success("修改成功！");
        }else{
            return Result.error("修改失败！");
        }
    }

    @GetMapping("/findAllConsult")
    public Result findAllConsult(Consult consult){
        List<Consult> consults = consultService.findAll(consult);
        return Result.success(consults);
    }

    @GetMapping("/findByIdConsult/{consultId}")
    public Result findByIdConsult(@PathVariable("consultId") Long consultId){
        Consult consult = consultService.findById(consultId.intValue());
        return Result.success(consult);

    }


    @GetMapping("/findByPageConsult")
    public Result findByPageConsult(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "5")Integer pageSize,Consult consult){
        PageInfo<Consult> pageInfo = consultService.findByPage(pageNum, pageSize, consult);
        return Result.success(pageInfo);

    }

}
